package projectTurtle;

import java.sql.*;
/**My SQL driver class
 *This class will allow user to construct object of this class
 *BY sending threw a few vital arguements it will
 *establish a connection with the driver which
 *will allow one to communicate to the database
 *using structure query langue
 *Arguments required in constructor:*String-to establish connection with driver ---Class.forName()
 *                                  *URL-ip Address to construct a connection with a certain database
 *                                  *User name & password- to gain access to database
 *                                  *  
 *
 */


public class MySQLdriverConnection 
{
	//attributes
	private String theDriverPath;                  //refers to the driver we are using to comunicate with the MYSQL
	private String theURL;                         //refers to the db that we are attempting to access
	private String userName,password;
	private String dataB;                          //The database we make connection with
	private String ipAdd;                          //ipAddress 
	private Connection myConn;
	
	//construct MySQLdriverConnection
	public MySQLdriverConnection(String inUserName,String inPassword,String ipAddress,String dbAccess) throws SQLException,ClassNotFoundException
	{
		theDriverPath = "com.mysql.jdbc.Driver";
		ipAdd = ipAddress;
		dataB = dbAccess;
		theURL = "jdbc:mysql://" + ipAdd + "/" + dataB;
		userName = inUserName;
		password = inPassword;
		
		//establishing a connection with the Driver we will use to comunicate with
		//the SQL DATABASE
		Class.forName(theDriverPath);
		
		//creating a connection to the Specified DataBase.
		//connection must be closed when all activity with it is complete
		//using the conection.close(); method
		
		//We use a static method from the DriverManager class
		//to make a connection.... Connection = DriverManager.getConnection(URL,userName,Password
	
		myConn = DriverManager.getConnection(theURL,userName,password);
		System.out.println("Connection to \" "+ theURL + " was successful");
		
	}//end of constructing MySQLdriverConnection
	
	//GENERAL METHODS**********************************************************************************************************************
	//*************************************************************************************************************************************
		
		//mutator method for changing the current driver
		public void setDriver(String newDriver)
		{
			theDriverPath = newDriver;
		}//end setDriver Method
	
		//mutator methos for changing the URL/ip address.to change the DataBase
		public void setURL(String newURL)
		{
			theURL = newURL;
		}//end setURL
	
		//accessor method used to get the Driver in use
		public String getDriver()
		{
			return theDriverPath;
		}//end getDriver
	
		//accessor method used to get The ip address of database being used
		public String getURL()
		{
			return theURL;
		}//end getIpAddress
		
		public Connection getConnection()
		{
			return myConn;
		}
		
		public void closeConn() 
		{
			try
			{
				myConn.close();
			}
			catch(SQLException sqlE)
			{
				System.out.println("Connection Could not be closed");
				sqlE.printStackTrace();
			}	
		}
		
	//end of general methods****************************************************************************************************************
	//**************************************************************************************************************************************
	   
}//end MySQLdriver Connection